function [norm_out, local_max_avg, norm_factor, lm_count, global_max] = maxnorm_sim_2(img_in)

% the normalization code (from HRL) ignores border pixels, but the hardware
    % considers border pixels, so pad the array before calling HRL code 
    img = padarray(img_in, [1,1]);

    global_max = max(img(:));
    %img = img ./ maxVal;
    [local_max_avg, lm_count] = LocalMaxStats(img);
    norm = (global_max - local_max_avg);
    norm_factor = (norm*norm)/(global_max^3); %norm factor = (M-u)^2/M
    norm_out_tmp = norm_factor*img;
    
    % remove the padding
    norm_out = norm_out_tmp(2:(end-1),2:(end-1));
end